/*
  Losu Copyright Notice
  --------------------
    Losu is an open source programming language project under the MIT license
  that can be used for both academic and commercial purposes. There are no
  fees, no royalties, and no GNU-like "copyright" restrictions. Losu qualifies
  as open source software. However, Losu is not public property, and founder
  'chen-chaochen' retains its copyright.

    Losu has been registered with the National Copyright Administration of the
  People's Republic of China, and adopts the MIT license as the copyright
  licensing contract under which the right holder conditionally licenses its
  reproduction, distribution, and modification rights to an unspecified public.

    If you use Losu, please follow the public MIT agreement or choose to enter
  into a dedicated license agreement with us.

  The MIT LICENSE is as follows
  --------------------
  Copyright  2020 - now  chen-chaochen

    Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the “Software”), to
  deal in the Software without restriction, including without limitation the
  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  sell copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/

/*
    This document is part of the Losu Community OSPP - 2024 subject results
  contributed by Dongqiao Huang @ NEUQ, following MIT LICENSE Contributed to
  the Losu Community.

  The MIT LICENSE is as follows
  --------------------
  Copyright 2024 Dongqiao Huang

    Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the “Software”), to
  deal in the Software without restriction, including without limitation the
  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  sell copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
 */

#ifndef HLEXER_HH
#define HLEXER_HH

#include <cstdint>
#include <sstream>
#include <string>

namespace LosuPreCompiler
{
namespace Lexer
{

class hLexer
{
public:
  hLexer (std::string source, std::string filename); // constructor.
  void nextToken ();                                 // Move to the next token.
  std::int32_t getLine ();   // Get the current line number.
  std::int32_t getTkType (); // Get the type of the current token.
  std::string getTkValue (); // Get the value of the current token.

  // declare segment
  enum synTktype
  {
    // tkType is int16_t, more than ASCII
    TK_EOZ = 256,
    TK_MODULE,
    TK_IMPORT,
    TK_SUB,
    TK_DEF,
    TK_OBJECT,
    TK_CLASS,
    TK_KSTR,
    TK_STRING,
    TK_NUMBER,
    TK_SCRIPT,
    TK_POINT,
    TK_NAME,
    TK_END,
  } synTktype;
  struct synTklist_t
  {
    std::int32_t tkType;
    const char *tkValue;
    std::int8_t isKey;
  } synTklist[TK_END - 255] = {
    { TK_EOZ, "EOZ", 0 },       { TK_MODULE, "module", 2 },
    { TK_IMPORT, "import", 1 }, { TK_SUB, "sub", 2 },
    { TK_DEF, "def", 2 },       { TK_OBJECT, "object", 2 },
    { TK_CLASS, "class", 2 },   { TK_STRING, "string", 0 },
    { TK_KSTR, "string", 1 },   { TK_NUMBER, "number", 1 },
    { TK_SCRIPT, "script", 1 }, { TK_POINT, "pointer", 1 },
    { TK_NAME, "name", 0 },     { TK_END, "end", 0 },
  };

private:
  std::int32_t CurChar (); // Get the current character.
  void NextChar ();        // Move to the next character.
  void PushIndent ();      // Push indent.
  void PopIndent ();       // Pop indent.
  std::string ReadName (); // Read Name.
  std::string ReadString (std::int32_t c, std::int32_t line); // Read String.

protected:
  // syntax segment
  std::stringstream source; // Source code stream.
  std::string fName;        // File name.
  std::string sSrc;         // Source code.
  std::size_t sPos;         // Position.
  std::size_t sLen;         // Length.
  std::int32_t sLine;       // Line number.
  std::int32_t sEOZ;        // End of the source code.

  // data segment
  std::int32_t tkType; // Token type.
  std::string tkValue; // Token value.
  struct synIdt        // Indent structure.
  {
    std::int32_t nowIdt; // Now indent (should be).
    std::int32_t curIdt; // Current indent.
    std::int32_t size;   // Indent size. 2/4/8
    std::int32_t idtNum; // Indent number.
  } synIdt;
};

}
}
#endif
